package ods_map

import (
	"server/global"
	"server/model/common/request"
)

type DbFldMapService struct {
}

func (dbFldMapService *DbFldMapService) CreateDbFldMap(dbFldMap *DbFldMap) (err error) {
	err = global.SysDB.Create(dbFldMap).Error
	return err
}

func (dbFldMapService *DbFldMapService) DeleteDbFldMap(dbFldMap DbFldMap) (err error) {
	err = global.SysDB.Delete(&dbFldMap).Error
	return err
}

func (dbFldMapService *DbFldMapService) DeleteDbFldMapByIds(ids request.IdsReq) (err error) {
	err = global.SysDB.Delete(&[]DbFldMap{}, "id in ?", ids.Ids).Error
	return err
}

func (dbFldMapService *DbFldMapService) UpdateDbFldMap(dbFldMap DbFldMap) (err error) {
	err = global.SysDB.Save(&dbFldMap).Error
	return err
}

func (dbFldMapService *DbFldMapService) GetDbFldMapById(id uint) (dbFldMap DbFldMap, err error) {
	err = global.SysDB.Where("id = ?", id).First(&dbFldMap).Error
	return
}

func (dbFldMapService *DbFldMapService) GetDbFldMapInfoList(info DbFldMapSearch) (list []DbFldMap, total int64, err error) {
	limit := info.PageSize
	offset := info.PageSize * (info.Page - 1)
	db := global.SysDB.Model(&DbFldMap{})
	var dbFldMaps []DbFldMap
	if info.StartCreatedAt != nil && info.EndCreatedAt != nil {
		db = db.Where("created_at BETWEEN ? AND ?", info.StartCreatedAt, info.EndCreatedAt)
	}
	err = db.Count(&total).Error
	if err != nil {
		return
	}

	err = db.Limit(limit).Offset(offset).Order("srcDbType asc,tgtDbType asc,srcFldType asc,tgtFldType asc").Find(&dbFldMaps).Error
	return dbFldMaps, total, err
}

func (dbFldMapService *DbFldMapService) GetAllDbFldMaps() (dbFldMap []DbFldMap, err error) {
	err = global.SysDB.Find(&dbFldMap).Error
	return dbFldMap, err
}

func (dbFldMapService *DbFldMapService) GetDbFldMap() (dbFldMap []DbFldMap, err error) {
	err = global.SysDB.Find(&dbFldMap).Error
	return dbFldMap, err
}
